from fastapi import Request

from service import UserService, SongService
from common.result.Result import Result
from common.enum import BaseEnum
from util.log import Log
from util.str import Str
from util.token import Token


async def GetDailyRecommendSongList(req: Request):
    try:
        token = req.headers.get("token")
        
        user = await UserService.getUserByToken(token)
        
        songList = await SongService.getDailyRecommendSongList()

        if user != None:
            # 获取收藏信息
            songIdList = [item.get("id") for item in songList]
            favoriteList = await SongService.getSongFavoriteList(songIdList, user.id)
            # 构造 收藏信息
            for song in songList:
                song.update(isFavorite=False)
                for favorite in favoriteList:
                    if favorite.song_id == song.get("id"):
                        song.update(isFavorite=True)

        return Result.success(songList)
    except Exception as e:
        Log.error('获取每日推荐歌曲异常', e)
        return Result.error(BaseEnum.SERVICE_ERROR)



